﻿@model LeadMaster.ViewModels.ViewModelOutsourcer

@{
    ViewBag.Title = "Questionnaire";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="spacer"></div>
<label style="color: #808080; font-family: Arial, Helvetica, sans-serif; border-radius: 8px; font-weight: bolder; font-variant: normal; border-style: solid; border-color: #800000; font-size: xx-large;">
@Model.Leads.IntRef.Trim().ToUpper()  @DateTime.Now.ToShortDateString()
</label>
<div class="spacer"></div>
@using (Html.BeginForm("EditQuestionnaire", "Outsourcer", FormMethod.Post, new { id = "frmEditQuestionnaire" }))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(m => m.Leads.LeadID)
@Html.HiddenFor(m => m.CallCentreID)
@Html.HiddenFor(m => m.BlockID)
@Html.HiddenFor(m => m.LeadStatus.PurchaseOrderID)
    
<fieldset style="width: 650px">

<legend>Enter New Lead - Mandatory questions are in blue</legend>
<table style="width: 650px">
@{
    int questionCounter = 0;
    int curPrimary = 0;
    int curSecondary = 0;
    int previousLeadGroupsID = 0;
    foreach (var item in Model.LeadQuestions)
    {
        if (item.IsQuestionHeader)
        {
            if (previousLeadGroupsID != 0)
            {
                <text>
                <script type="text/javascript">
                $(document).ready(function () {
                    $('#questionlabel@(previousLeadGroupsID)').attr('data-curPrimary', '@(curPrimary)');
                    $('#questionlabel@(previousLeadGroupsID)').attr('data-curSecondary','@(curSecondary)');
                });
                </script>
                </text>
            }
            if (item.IsQuestionHeader == true && item.QualifyingGroup == true)
            {
                curPrimary = 0;
                curSecondary = 0;
                previousLeadGroupsID = item.LeadGroupsID;
            }
            <tr>
            <th colspan="2" class="questionnaireheaderrow">
            <label style="font-family: Arial, Helvetica, sans-serif; font-size: large; font-weight: bold; color: #666666" 
            @if (item.IsQuestionHeader == true && item.QualifyingGroup == true)
            {
		        @:class="qualifyinggroup" data-id="@(item.LeadGroupsID)" data-primary="@(item.MinMandatoryPrimary)" data-secondary="@(item.MinMandatorySecondary)" data-primarysecondary="@(item.PrimarySecondary)" 
                @:data-curPrimary="0" data-curSecondary="0"
                @:id="questionlabel@(item.LeadGroupsID)"
	        } >@item.QuestionHeader</label>
            @if (@item.SubHeader != null)
            {
                <label style="font-family: Arial, Helvetica, sans-serif; font-size: small; font-weight: bold; color: #800000">@item.SubHeader</label>
            }
            @if (@item.Notes != null)
            {
                <div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; color: #F2A44D; font-style: italic;">@item.Notes</div>
            }            
            </th>
            </tr>
        }
        else
        {
            <tr>
            <td>
            <label @if (@item.Mandatory == true)
                   {
                        @:style="color: #0000FF" 
                    }>@item.TheQuestion</label>
            @if (@item.Notes != null)
            {
                <div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; color: #F2A44D; font-style: italic;">@item.Notes</div>
            }            

            </td>
            <td>
            <input type="hidden" name="LeadQuestions[@(questionCounter.ToString())].TheQuestion" value="@(item.TheQuestion)" />
            <input type="hidden" name="LeadQuestions[@(questionCounter.ToString())].Mandatory" value="@(item.Mandatory.ToString())" />
            <input type="hidden" name="LeadQuestions[@(questionCounter.ToString())].QuestionType" value="@(item.QuestionType.ToString())" />
            <input type="hidden" name="LeadQuestions[@(questionCounter.ToString())].LeadStatQuestionsID" value="@(item.LeadStatQuestionsID.ToString())" />
            <input type="hidden" name="LeadQuestions[@(questionCounter.ToString())].LeadGroupQuestionsID" value="@(item.LeadGroupQuestionsID.ToString())" />
            <input type="hidden" name="LeadQuestions[@(questionCounter.ToString())].CompanyRegisteredID" value="@(item.CompanyRegisteredID.ToString())" />
            <input type="hidden" name="LeadQuestions[@(questionCounter.ToString())].CompanyRestrictionsID" value="@(item.CompanyRestrictionsID.ToString())" />
            <input type="hidden" name="LeadQuestions[@(questionCounter.ToString())].CompanySundriesID" value="@(item.CompanySundriesID.ToString())" />
            <input type="hidden" name="LeadQuestions[@(questionCounter.ToString())].DisplayOrder" value="@(item.DisplayOrder.ToString())" />
            @switch (item.QuestionType)
            {
                case "C":
                    if (item.DropDown == true && item.AcceptedAnswerChar != null)
                    {
                        <select id="question[@(questionCounter)]" name="LeadQuestions[@(questionCounter)].TheAnswer" style="width:300px;"
                        @if (item.QualifyingGroup==true)
    	                {
	    	                @:class="chosen qualifyinggroupquestion textqualifyinggroupquestion @(Model.LeadTemplate.LinkedGroupSets??false ? "linkedgroupsets" : "")" data-leadgroupid="@(item.LeadGroupsID)"
                            if (item.PrimarySecondary=="P" )
                            {
                                @:data-primarysecondary="P"
                            }
                            else
                            {
                                @:data-primarysecondary="S"
                            }
	                    }
                        else
                        {
                         @:class="chosen"   
                        }
                        @if (item.Mandatory == true)
                        {
		                @:required
	                    }
                        >
                        <option value=""></option>
                        @foreach (var answeritems in item.AcceptedAnswerChar.Split(','))
                        {
                            <option value="@(answeritems)"
                            @if (@answeritems == item.TheAnswer)
                            {  
                             @:selected   
                             if (item.PrimarySecondary == "P")
                             {
                                 curPrimary++;
                             }
                             else
                             {
                                 curSecondary++;
                             }
                            }
                            >@answeritems</option>
                        }
                        </select>
                    }
                    else
                    {
                        <input id="question[@(questionCounter)]" type="text" name="LeadQuestions[@(questionCounter)].TheAnswer" 
                        
                        @if (item.QualifyingGroup==true)
	                    {
		                    @:class="largetextbox qualifyinggroupquestion textqualifyinggroupquestion @(Model.LeadTemplate.LinkedGroupSets??false ? "linkedgroupsets" : "")" data-leadgroupid="@(item.LeadGroupsID)"
                            if (item.PrimarySecondary=="P" )
                            {
                                @:data-primarysecondary="P"
                            }
                            else
                            {
                                @:data-primarysecondary="S"
                            }
	                    }
                        else
                        {
                            if (item.TheQuestion == "Landline/Primary Number")
                            {
                                @:class="telephonetextbox"
                            }
                            else if(item.TheQuestion.Contains("Postcode"))
                            {
                                @:class="postcodetextbox"
                            }
                            else
                            {
                                @:class="largetextbox"
                            }
                        } 
                        @if (item.Mandatory == true)
                        {
		                @:required
	                    }
                        value="@(item.TheAnswer)"
                        />
                    }
                    break;
                case "L":
                    <text>
                    <input type="checkbox" name="LeadQuestions[@(questionCounter)].TheAnswer" value="true" data-checkboxtype="yes"
                    @if (item.QualifyingGroup == true)
                    {
		                @:class="qualifyinggroupquestion checkqualifyinggroupquestion chosen" data-leadgroupid="@(item.LeadGroupsID)"
                        if (item.PrimarySecondary == "P")
                        {
                            @:data-primarysecondary="P"
                            if (item.TheAnswer != null && item.TheAnswer == "Yes")
                            {
                                curPrimary++;
                            }
                        }
                        else
                        {
                            @:data-primarysecondary="S"
                            if (item.TheAnswer != null && item.TheAnswer == "Yes")
                            {
                                curSecondary++;
                            }
                        }
                    }
                    else
                    {
                        @:class="chosen"   
                    }
                    @if (item.Mandatory == true)
                    {
		            @:required 
	                } 
                    @if (item.TheAnswer != null && item.TheAnswer == "Yes")
                    {
		                @:checked
                	}
                    />
                    <input name="LeadQuestions[@(questionCounter)].TheAnswer" type="hidden" value="false">            
                    </text>
                                           break;
                case "I":
                    <text>
                        <select id="question[@(questionCounter)]" name="LeadQuestions[@(questionCounter)].TheAnswer" style="width:300px;"
                        @if (item.QualifyingGroup == true)
                        {
	    	                @:class="largetextbox qualifyinggroupquestion textqualifyinggroupquestion chosen @(Model.LeadTemplate.LinkedGroupSets ?? false ? "linkedgroupsets" : "")" data-leadgroupid="@(item.LeadGroupsID)"
                            if (item.PrimarySecondary == "P")
                            {
                                @:data-primarysecondary="P"
                            }
                            else
                            {
                                @:data-primarysecondary="S"
                            }
                        }
                        else
                        {
                         @:class="chosen"   
                        }
                        @if (item.Mandatory == true)
                        {
		                @:required
	                    }
                        >
                        <option value=""></option>
                        @for (int i = item.AcceptedAnswerIntFrom; i <= item.AcceptedAnswerIntTo; i++)
                        {
                            <option value="@(i.ToString())">@i.ToString()</option>
                        }
                        </select>
                    </text>
                        break;
                default:
                        break;
            }
            </td>
            </tr>
            questionCounter++;
        }

    }
}
</table>
<p>
<input type="submit" value="Submit Lead" class="bigsubmit" id="submitlead" />
</p>
</fieldset>
}

<input type="hidden" id="hdncurrenttext" value="" />

<script type="text/javascript">

    $("#frmQuestionnaire").validate();

    $(".chosen").chosen();

    $(".telephonetextbox").blur(function () {
        $.ajax({
            type: "GET",
            url: '@(Url.Action("CheckDuplicate", "Outsourcer"))/?telephoneNumber=' + $(this).val(),
            success: function (data) {
                if (data.toString().length > 0) {
                    alert(data.toString());
                }
            }
        });
    });

    $(".postcodetextbox").blur(function () {
        $.ajax({
            type: "GET",
            url: '@(Url.Action("CheckPostcode", "Outsourcer"))/?postCode=' + $(this).val() + '&leadid=@(Model.Leads.LeadID)',
            success: function (data) {
                if (data.toString().length > 0) {
                    alert("Regret to inform you that this postcode is not in this Client's Territory");
                    $('.postcodetextbox').val('');
                }
            }
        });
    });


    $("#submitlead").click(function () {
        $(".qualifyinggroupquestion").attr("disabled", false);
        return true;
    });

    $('input[type="checkbox"]').click(function () {
        if ($(this).attr("class").indexOf('checkqualifyinggroupquestion') > 0) {
            if ($(this).is(':checked') == true) {
                CheckValidation(this, "true");
            }
            else {
                CheckValidation(this, "false");
            }

        }
    });

    $(".textqualifyinggroupquestion").focus(function () {
        $("#hdncurrenttext").val($(this).val());
        $("#hdncurrentid").val($(this).attr('id'));
    });

    $(".textqualifyinggroupquestion").blur(function () {
    var leadgroupid = $(this).data("leadgroupid");
    var selector = ".qualifyinggroup[data-id='" + leadgroupid + "']"
    var checknewval;
    var qualifiedLabel;
    if ($(this).data("primarysecondary") == "P") {
        checknewval = $(selector).attr("data-curPrimary");
        if ($(this).val().trim().length > 0 && $("#hdncurrenttext").val() == "" ) {
            checknewval++;
        }
        else if ($(this).val().trim().length == 0 && $("#hdncurrenttext").val() != "" ) {
            checknewval--;
        }
        $(selector).attr("data-curPrimary", checknewval);
    }
    else {
        checknewval = $(selector).attr("data-curSecondary");
        if ($(this).val().trim().length > 0 && $("#hdncurrenttext").val() == "") {
            checknewval++;
        }
        else if ($(this).val().trim().length == 0 && $("#hdncurrenttext").val() != "") {
            checknewval--;
        }
        $(selector).attr("data-curSecondary", checknewval);
    }
    if ($(selector).attr("data-cursecondary") == $(selector).attr("data-secondary")) {
        if ($(selector).attr("data-primary") == $(selector).attr("data-curprimary")) {
            alert($(selector).html() + " qualified");
            @if (@Model.LeadTemplate.LinkedGroupSets != true)
            {                 
                @:$(".qualifyinggroupquestion").attr("disabled", "disabled");
            }
            $(this).attr("disabled", false);
            $("#submitlead").attr("disabled", false);
        }
        else {
            $(".qualifyinggroupquestion").attr("disabled", false);
            $("#submitlead").attr("disabled", true);
        }
    }
    else {
        $(".qualifyinggroupquestion").attr("disabled", false);
        $("#submitlead").attr("disabled", true);
    }
    });

    function CheckValidation(currentObject, ischecked)
    {
        var leadgroupid = $(currentObject).data("leadgroupid");
        var selector = ".qualifyinggroup[data-id='" + leadgroupid + "']"
        var checknewval;
        var qualifiedLabel;
        if ($(currentObject).data("primarysecondary") == "P") {
            checknewval = $(selector).attr("data-curPrimary");
            if (ischecked=="true") {
                checknewval++;
            }
            else {
                checknewval--;
            }
            $(selector).attr("data-curPrimary", checknewval);
            checknewval = $(selector).attr("data-curPrimary");
        }
        else {
            checknewval = $(selector).attr("data-cursecondary");
            if (ischecked == "true") {
                checknewval++;
            }
            else {
                checknewval--;
            }
            $(selector).attr("data-cursecondary", checknewval);
        }
        if ($(selector).attr("data-cursecondary") == $(selector).attr("data-secondary")) {
            if ($(selector).attr("data-curprimary") >= $(selector).attr("data-primary")) {
                alert($(selector).html() + " qualified");
                @if (@Model.LeadTemplate.LinkedGroupSets != true)
                {                 
                @:$(".qualifyinggroupquestion").attr("disabled", "disabled");
                }
                $(currentObject).attr("disabled", false);
                $("#submitlead").attr("disabled", false);
            }
            else {
                $(".qualifyinggroupquestion").attr("disabled", false);
                $("#submitlead").attr("disabled", true);
            }
        }
    }

    $(".chosen").on('change', function(evt,params) {
        var leadgroupid = $(this).data("leadgroupid");
        var selector = ".qualifyinggroup[data-id='" + leadgroupid + "']"
        var checknewval;
        var qualifiedLabel;
        if ($(this).data("primarysecondary") == "P") {
            checknewval = $(selector).attr("data-curPrimary");
            if ($(this).val().trim().length > 0 && $("#hdncurrenttext").val() == "" ) {
                checknewval++;
            }
            else if ($(this).val().trim().length == 0 && $("#hdncurrenttext").val() != "" ) {
                checknewval--;
            }
            $(selector).attr("data-curPrimary", checknewval);
        }
        else {
            checknewval = $(selector).attr("data-curSecondary");
            if ($(this).val().trim().length > 0 && $("#hdncurrenttext").val() == "") {
                checknewval++;
            }
            else if ($(this).val().trim().length == 0 && $("#hdncurrenttext").val() != "") {
                checknewval--;
            }
            $(selector).attr("data-curSecondary", checknewval);
        }
        if ($(selector).attr("data-cursecondary") == $(selector).attr("data-secondary")) {
            if ($(selector).attr("data-curprimary") >= $(selector).attr("data-primary")) {
                alert($(selector).html() + " qualified");
                @if (@Model.LeadTemplate.LinkedGroupSets != true)
                {                 
                    @:$(".qualifyinggroupquestion").attr("disabled", "disabled");
                }
                $(this).attr("disabled", false);
                $("#submitlead").attr("disabled", false);
            }
            else if($(selector).hasClass("qualifyinggroupquestion"))
            {
                $(".qualifyinggroupquestion").attr("disabled", false);
                $("#submitlead").attr("disabled", true);
            }
        }
        else
        {
            $(".qualifyinggroupquestion").attr("disabled", false);
            $("#submitlead").attr("disabled", true);
        }
    });

    $(".textqualifyinggroupquestion").on('chosen:showing_dropdown', function(evt,params) {
        $("#hdncurrenttext").val($(this).val());
        $("#hdncurrentid").val($(this).attr('id'));
    });
</script>

